package com.fau.wm.action.index;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalOrdersWaitDelivery;
import com.fau.wm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 订单发货Action
 * @author kangyj
 * @datetime 2014年11月28日,下午8:41:14
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class OrdersDeliveryAction extends PublicAction implements
		ModelDriven<MedicalOrdersWaitDelivery> {

	private static final long serialVersionUID = 1221227418375896386L;
	
	private MedicalOrdersWaitDelivery ordersWaitDelivery = new MedicalOrdersWaitDelivery();
	
	/**
	 * @desc 订单发货页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月28日,下午8:42:47
	 */
	@Action(value = "orders_delivery_init", results = {@Result(name = SUCCESS, location = "orders_delivery_init.jsp")})
	public String init() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询待发货订单
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月28日,下午10:10:05
	 */
	@Action(value = "orders_delivery_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ordersWaitDelivery.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getOrdersManagerService().findOrdersWaitDelivery(ordersWaitDelivery, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 导入发货页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月29日,上午12:09:33
	 */
	@Action(value = "orders_delivery_import_init", results = {@Result(name = SUCCESS, location = "orders_delivery_import_init.jsp")})
	public String importDeliveryInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 导入发货
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月29日,上午12:10:55
	 */
	@Action(value = "orders_delivery_import")
	public String importDelivery() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request);
		try {
			String row = this.baseAllService.getOrdersManagerService().importDelivery(admin, file);
			String rowCount[] = row.split(",");
			map.put("rightCount", rowCount[0]);
			map.put("errorCount", rowCount[1]);
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询物流公司
	 * @return null
	 * @author kangyj
	 * @datetime 2014年12月6日,下午6:06:44
	 */
	@Action(value = "orders_delivery_logistics_view")
	public String logisticsView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getOrdersManagerService().findLogisticsList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 下载订单发货导入模版
	 * @return null
	 * @author zhaoey
	 * @datetime 2015年4月7日,上午10:00:07
	 */
	@Action(value = "orders_delivery_download")
	public String downloadDeliveryTemp() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"导入订单发货模板.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getOrdersManagerService().downloadDeliveryTemp(user);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 订单发货
	 * @return null
	 * @author kangyj
	 * @datetime 2014年12月6日,下午7:44:07
	 */
	@Action(value = "orders_delivery")
	public String delivery() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(ordersWaitDelivery.getId())
				&& StringUtils.isNotBlank(ordersWaitDelivery.getExpressId())
				&& StringUtils.isNotBlank(ordersWaitDelivery.getExpressNo())
				&& StringUtils.isNotBlank(ordersWaitDelivery.getWarhouseId())) {
			String flag = this.baseAllService.getOrdersManagerService().delivery(ordersWaitDelivery);
			if(flag.equals("1")) {
				map.put("success", "true");
			} else if(flag.equals("0")) {
				map.put("success", "fail");
			} else {
				map.put("success", "empty");
				map.put("msg", flag);
			}
		} else {
			map.put("success", "false");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单发货-导出当前页
	 * @return null
	 * @author liy
	 * @datetime 2014年12月26日,下午2:20:11
	 */
	@Action(value = "orders_delivery_export_current")
	public String currentExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"订单发货导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getOrdersManagerService().exportOrdersCurrent(this.ordersWaitDelivery.getId());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 订单发货-导出所有
	 * @return null
	 * @author liy
	 * @datetime 2014年12月26日,下午4:01:58
	 */
	@Action(value = "orders_delivery_export_all")
	public String allExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"订单发货导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getOrdersManagerService().exportOrdersAll(this.ordersWaitDelivery);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalOrdersWaitDelivery getModel() {
		return ordersWaitDelivery;
	}
	
	
}
